package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "管理端-菜品接口")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 清除缓存
     * @param pattern 符合键的模板
     */
    private void cleanCache(String pattern) {
        //获取符合条件的Redis中的键
        Set keys = redisTemplate.keys(pattern);
        //直接删除符合条件的键值对数据
        redisTemplate.delete(keys);//多态
    }


    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}",dishDTO);
        //访问Service层完成业务
        dishService.saveWithFlavor(dishDTO);
        //清理缓存
        cleanCache("dish_"+dishDTO.getCategoryId());
        log.info("id为{}的分类菜品缓存进行清理",dishDTO.getCategoryId());

        //返回结果
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        //调用业务层完成逻辑
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        //返回结果
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除菜品：{}",ids);
        //调用业务层完成逻辑
        dishService.deleteBatch(ids);
        //清理缓存
        cleanCache("dish_*");
        log.info("批量删除菜品，清理所有缓存");

        //返回结果给前端
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品及口味数据")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品：id={}",id);
        //访问业务层得到数据
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        //返回给前端
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品，包含口味修改")
    public Result update(@RequestBody DishDTO dishDTO) {

        log.info("修改菜品：{}",dishDTO);
        //调用服务层完成逻辑
        dishService.updateDishWithFlavor(dishDTO);

        //清理缓存
        cleanCache("dish_*");
        log.info("菜品有更新，清理所有缓存");

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }
}
